package com.mjf.cjt.event.handler;

import com.mjf.cjt.domain.TicketEntity;
import com.mjf.cjt.event.ChanjetMsg;
import com.mjf.cjt.event.EventHandler;
import com.mjf.cjt.event.content.AppTicketContent;
import com.mjf.cjt.mapper.CjtAuthMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * appTicket事件处理器
 *
 * @author: zsc
 * @create: 2020/11/5 6:02 下午
 **/
@Component("APP_TICKET")
@Slf4j
public class AppTicketHandler implements EventHandler<AppTicketContent> {

    @Autowired
    private CjtAuthMapper cjtAuthMapper;

    @Override
    @Transactional
    public Object execute(ChanjetMsg<AppTicketContent> chanjetMsg) {
        TicketEntity ticketEntity = cjtAuthMapper.getTicket(chanjetMsg.getAppKey());
        if (null == ticketEntity) {
            log.info("创建ticket:{}",chanjetMsg.getBizContent().getAppTicket());
            ticketEntity = new TicketEntity();
            ticketEntity.setAppKey(chanjetMsg.getAppKey());
            ticketEntity.setTicket(chanjetMsg.getBizContent().getAppTicket());
        }else{
            log.info("更新ticket:{}",chanjetMsg.getBizContent().getAppTicket());
            ticketEntity.setTicket(chanjetMsg.getBizContent().getAppTicket());
        }
        cjtAuthMapper.saveTicket(ticketEntity);
        return "success";
    }
}
